<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!--********************************************************************
* 该示例需要引入
* UGCWasm (https://iclient.supermap.io/web/libs/ugcwasm/1.0.1/UGCWasmAll.js)
*********************************************************************-->
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title data-i18n="resources.title_ugcGeodesicArea"></title>
    <style>
      body {
        margin: 0;
        overflow: hidden;
        background: #fff;
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
      }
      #toolbar {
        position: absolute;
        top: 50px;
        right: 10px;
        width: 280px;
        text-align: center;
        z-index: 10000;
        border-radius: 4px;
      }
    </style>
  </head>

  <body>
    <div id="toolbar" class="panel panel-primary">
      <div class="panel-heading">
        <h5 class="panel-title text-center">计算面积</h5>
      </div>
      <div class="panel-body content">
        <input type="button" class="btn btn-default" value="计算" onclick="search()" />
      </div>
    </div>
    <div id="map" style="width: 100%; height: 100%"></div>
    <script type="text/javascript" include="bootstrap-css" src="../js/include-web.js"></script>
    <script type="text/javascript" include="ugcwasm" src="../../dist/maplibregl/include-maplibregl.js"></script>
    <script>
      var popup, polygon;
      var map;
      var host = window.isLocal ? window.server : 'https://iserver.supermap.io';
      var url = host + '/iserver/services/map-world/rest/maps/World/zxyTileImage.png?z={z}&x={x}&y={y}';
      function initMap(url) {
        return new Promise((resolve) => {
          map = new maplibregl.Map({
            container: 'map',
            style: {
              version: 8,
              sources: {
                'raster-tiles': {
                  type: 'raster',
                  tiles: [url],
                  tileSize: 256
                }
              },
              layers: [
                {
                  id: 'simple-tiles',
                  type: 'raster',
                  source: 'raster-tiles'
                }
              ]
            },
            center: [120, 30],
            zoom: 6
          });
          map.on('load', function () {
            resolve(map);
          });
        });
      }

      function addFeatures() {
        polygon = {
          type: 'Feature',
          properties: {},
          geometry: {
            coordinates: [
              [
                [121.74162512536924, 32.60736281118376],
                [120.59633803460247, 32.42386901254892],
                [119.76027845834045, 31.47151252648368],
                [120.36728061644811, 29.925467312116822],
                [121.6843607708318, 29.93539273511712],
                [122.37153302529236, 30.627700930765172],
                [123.15032824701461, 31.66667145093632],
                [122.77238350706119, 32.597714586070424],
                [121.74162512536924, 32.60736281118376]
              ]
            ],
            type: 'Polygon'
          }
        };
        addLayer('fill1', 'fill', [polygon], {
          'fill-color': 'blue',
        });
      }

      function search() {
        clearPopup();
        var geometryAnalysis = new maplibregl.supermap.GeometryAnalysis();
        var area;
        area = geometryAnalysis.computeGeodesicArea(polygon);
        popup = new maplibregl.Popup()
          .setLngLat([121, 31])
          .setHTML('面积为: ' + area + '平方米')
          .addTo(map);
      }

      function clearPopup() {
        popup && popup.remove();
      }
      initMap(url).then((map) => {
        addFeatures();
      });

      function addLayer(id, type, sourceData, paint) {
        map.addLayer({
          id,
          type,
          source: {
            type: 'geojson',
            data: {
              type: 'FeatureCollection',
              features: sourceData
            }
          },
          paint
        });
      }
    </script>
  </body>
</html>
